const EventBus = require("../template")

function testEventBusBasic() {
  const eventBus = new EventBus()

  console.log(
    "1. 基本事件订阅测试:",
    (() => {
      let received = false
      eventBus.on('test', () => {
        received = true
      })
      eventBus.emit('test')
      return received === true
    })()
  )

  console.log(
    "2. 事件参数传递测试:",
    (() => {
      let receivedData = null
      eventBus.on('data', (data) => {
        receivedData = data
      })
      eventBus.emit('data', 'hello world')
      return receivedData === 'hello world'
    })()
  )

  console.log(
    "3. 多个参数传递测试:",
    (() => {
      let receivedArgs = []
      eventBus.on('multi', (...args) => {
        receivedArgs = args
      })
      eventBus.emit('multi', 1, 2, 3, 'test')
      return JSON.stringify(receivedArgs) === JSON.stringify([1, 2, 3, 'test'])
    })()
  )

  console.log(
    "4. 多个监听器测试:",
    (() => {
      let count = 0
      eventBus.on('count', () => count++)
      eventBus.on('count', () => count++)
      eventBus.on('count', () => count++)
      eventBus.emit('count')
      return count === 3
    })()
  )

  console.log(
    "5. 取消订阅测试:",
    (() => {
      let count = 0
      const callback = () => count++
      eventBus.on('unsubscribe', callback)
      eventBus.on('unsubscribe', () => count++)
      eventBus.off('unsubscribe', callback)
      eventBus.emit('unsubscribe')
      return count === 1
    })()
  )

  console.log(
    "6. 一次性订阅测试:",
    (() => {
      let count = 0
      eventBus.once('once', () => count++)
      eventBus.emit('once')
      eventBus.emit('once')
      return count === 1
    })()
  )

  console.log(
    "7. 移除所有监听器测试:",
    (() => {
      let count = 0
      eventBus.on('remove', () => count++)
      eventBus.on('remove', () => count++)
      eventBus.removeAllListeners('remove')
      eventBus.emit('remove')
      return count === 0
    })()
  )

  console.log(
    "8. 监听器数量测试:",
    (() => {
      eventBus.on('count1', () => {})
      eventBus.on('count1', () => {})
      eventBus.on('count2', () => {})
      const count1 = eventBus.listenerCount('count1')
      const count2 = eventBus.listenerCount('count2')
      const total = eventBus.listenerCount()
      return count1 === 2 && count2 === 1 && total === 3
    })()
  )

  console.log(
    "9. 事件名称获取测试:",
    (() => {
      eventBus.on('event1', () => {})
      eventBus.on('event2', () => {})
      eventBus.on('event3', () => {})
      const names = eventBus.eventNames()
      return names.length === 3 && names.includes('event1') && names.includes('event2') && names.includes('event3')
    })()
  )

  console.log(
    "10. 最大监听器数量测试:",
    (() => {
      eventBus.setMaxListeners(2)
      const max = eventBus.getMaxListeners()
      return max === 2
    })()
  )

  console.log(
    "11. this上下文事件总线测试:",
    (() => {
      const eventObj = {
        multiplier: 2,
        processEvent: function(data) {
          const bus = new EventBus()
          bus.on('process', (val) => {
            return val * this.multiplier
          })
          return bus.emit('process', data)
        }
      }
      return eventObj.processEvent(5) === 10
    })()
  )

  console.log(
    "12. 复杂对象this绑定事件总线测试:",
    (() => {
      const eventConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(data) {
          const bus = new EventBus()
          bus.on('process', (val) => {
            return val >= this.config.threshold
          })
          return bus.emit('process', data)
        }
      }
      return eventConfigObj.processWithConfig(5) === true
    })()
  )
}

testEventBusBasic()
